package com.cskaoyan.market.filter;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CORSFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // Access-Control-Allow系列都是跨域相关的头
        //允许来自于哪个主机、端口号的机器往当前主机发送请求
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:9527");
        //发送请求时，请求方法允许哪些
        response.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,PUT,DELETE");
        //发送请求时，允许携带的头信息
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,Authorization,Content-Type,X-CskaoyanMarket-Admin-Token,X-CskaoyanMarket-Token");
        //发送请求时，是否允许携带Cookie凭证
        response.setHeader("Access-Control-Allow-Credentials","true");
        filterChain.doFilter(request, response);
    }
}